<!DOCTYPE html>
<html>

<head>
	<title>Second-Hand Store Register</title>
	<link rel="stylesheet" type="text/css" href="css/style.css">
</head>

<body>
	<header>
		<h2>UIC Second-Hand Store</h2>
	</header>
	<main>
		<div class="content narrowbox">
			<h2>Register</h2>
			<hr>
			<!--display result-->
			<p class="infotext">
				<?php
				// include the DB connect file
				include "connectStoreDB.php";

				// step2: prepare sql
				$user = $_POST["usr"];
				$pwd = $_POST["pwd"];
				$cpwd = $_POST["cpwd"];


				$sql = "SELECT * FROM users WHERE username='$user'";

				// query
				$result = $conn->query($sql);

				if ($result->num_rows > 0) {
					//if the email address is already in the database, give a warning message
					echo "Warning! Username $user already exists!";
				} else {
					//if the email address is not in the database, add the username and password to the database
					if ($pwd == $cpwd) {
						$sql = "INSERT INTO users(username,password,usertype) VALUES('$user','$pwd','user')";
						mysqli_query($conn, $sql);
						echo "Registration Successful!<br><br>Go to <a href='LoginPage.html'>Login</a>";
					} else {
						echo "Warning! Password and Confirmation Password do not match!";
					}
				}
				//close connection
				mysqli_close($conn);
				?>
			</p>
			<br>
			<hr>
			<br>
			<p class="infotext">back to <a href="RegisterPage.html">Register</a></p>
		</div>
	</main>
</body>

</html>